<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<% response.setContentType("text/html; charset=UTF-8");%>
<% request.setCharacterEncoding("utf-8");%>
<%@ page import="java.sql.*" %>
<%@ page import="javax.xml.parsers.*" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="java.io.File" %>
<%
	Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(getServletContext().getRealPath("/") + "web.xml"));
	doc.getDocumentElement().normalize();
	String dbhost = doc.getElementsByTagName("host").item(0).getTextContent();
	String dbport = doc.getElementsByTagName("port").item(0).getTextContent();
	String dbuser = doc.getElementsByTagName("user").item(0).getTextContent();
	String dbpass = doc.getElementsByTagName("pass").item(0).getTextContent();
	String dbname = doc.getElementsByTagName("database").item(0).getTextContent();
	String dbconnString = "jdbc:mysql://" + dbhost + ":" + dbport + "/" + dbname + "?setUnicode=true&characterEncoding=UTF-8";
	Class.forName("com.mysql.jdbc.Driver").newInstance();
	Connection dbconn = DriverManager.getConnection(dbconnString, dbuser, dbpass);
	Statement query = dbconn.createStatement();
%>

<table id="box-table-a" width="100%">
	<thead><tr>
			<th scope='col'>Họ tên</th>
			<th scope='col'>Số CMND</th>
			<th scope='col'>Mã đề thi</th>
			<th scope='col'>Ngày, giờ bắt đầu</th>
			<th scope='col'>Ngày, giờ kết thúc</th>
			<th scope='col'>Điểm</th>
		</tr></thead>
	<tbody>
		<%
			int pagesize = 10; // số dòng/trang
			int currentpage = Integer.parseInt(request.getParameter("page")); // trạng hiện tại
			int start = (currentpage - 1) * pagesize; // dòng bắt đầu của trang
			int currentrow = 0; // số dòng/trang hiện tại
			String limit = " limit " + Integer.toString(start) + " , " + Integer.toString(pagesize);
			String type = request.getParameter("type");
			String maTS = request.getParameter("mats");
			String condition = null;
			String order = " order by thoigianbd desc ";
			if (type.equals("1")) // quản trị
			{
				condition = " 1=1 ";
			} else // thí sinh
			{
				condition = " thisinh = " + maTS;
			}
			String table = " baithi left join thisinh on thisinh = maTS ";
			ResultSet rs = query.executeQuery("select * from " + table + " where " + condition + order + limit);
			while (rs.next()) {
				currentrow++;
				out.println("<tr>");
				out.println("<td>" + rs.getString("hoten") + "</td>");
				out.println("<td style='text-align: center;'>" + rs.getString("cmnd") + "</td>");
				out.println("<td style='text-align: right;'><a href='baithi.jsp?mabt=" + Integer.toString(rs.getInt("maBT")) + "'>" + Integer.toString(rs.getInt("dethi")) + "</a></td>");
				out.println("<td style='text-align: center;'>" + rs.getTimestamp("thoigianbd").toString() + "</td>");
				out.println("<td style='text-align: center;'>" + rs.getTimestamp("thoigiankt").toString() + "</td>");
				if (rs.getFloat("diem") == -1) // bài thi phạm qui
				{
					out.println("<td style='text-align: right;'><i style='color:red'>phạm qui</i></td>");
				} else {
					out.println("<td style='text-align: right;'>" + Float.toString(rs.getFloat("diem")) + "</td>");
				}
				out.println("</tr>");
			}
			rs.close();
		%>
	</tbody>
</table>
<div id="pagenav">
	<%
		String site = "thongke.jsp"; // site liên kết khi chọn page
		int stepNumPage = 10; // số lượng trang hiển thị trước và sau currentpage

		rs = query.executeQuery("select count(*) as numrow from " + table + " where " + condition);
		rs.next();
		int numrow = rs.getInt("numrow");
		rs.close();
		int numpage = (int) Math.ceil((double) numrow / pagesize);

		// danh sách các page
		String nav = "";
		for (int i = Math.max(currentpage - stepNumPage, 1); i <= Math.min(currentpage + stepNumPage, numpage); i++) {
			if (i == currentpage) {
				nav += "<span class='current'>" + Integer.toString(i) + "</span>";
			} else {
				nav += "<span><a href='" + site + "?page=" + Integer.toString(i) + "'>" + Integer.toString(i) + "</a></span>";
			}
		}

		// first, prev
		String first, prev;
		if (currentpage > 1) {
			int p = currentpage - 1;
			first = "<span class='first'><a href='" + site + "?page=1'> << first </a></span>";
			prev = "<span class='prev'><a href='" + site + "?page=" + Integer.toString(p) + "'> < prev </a></span>";
		} else {
			first = "<span class='disabled first'> << first </span>";
			prev = "<span class='disabled prev'> < prev </span>";
		}

		// next, last
		String next, last;
		if (currentpage < numpage) {
			int p = currentpage + 1;
			next = "<span class='next'><a href='" + site + "?page=" + Integer.toString(p) + "'> next > </a></span>";
			last = "<span class='last'><a href='" + site + "?page=" + Integer.toString(numpage) + "'> last >> </a></span>";
		} else {
			next = "<span class='disabled next'> next > </span>";
			last = "<span class='disabled last'> last >> </span>";
		}

		out.println("Page " + Integer.toString(currentpage) + " of " + Integer.toString(Math.max(1,numpage))
				+ ", showing " + Integer.toString(Math.min(currentrow, pagesize)) + " records out of " + Integer.toString(numrow) + " total"
				+ ". starting on record " + Integer.toString(start) + ", ending on " + Integer.toString(Math.min(start + currentrow, start + pagesize))
				+ "<br/>");
		out.println(first + prev + nav + next + last);
	%>
</div>
<% dbconn.close();%>